package com.milhouz.wormdroid.gl.model;

import android.util.Log;

import com.milhouz.wormdroid.R;
import com.milhouz.wormdroid.util.StaticValues;



/**
 * Wormdroid Target sprite
 */
public class TargetGLSprite extends NoPhysicGLSprite {
	
	public static final int DEFAULT_TARGET_X = 50;
	public static final int DEFAULT_TARGET_Y = 50;
	private static final int DEFAULT_TARGET_SPEED = 1;
	
	private int targetX;
	private int targetY;
	
	public boolean isLaunched = false;
	
	public TargetGLSprite() {
		super(9999, 0, 0, 10, 10, R.drawable.target);
		targetX = DEFAULT_TARGET_X;
		targetY = 0;
	}
	
	public void launchWeapon() {
		Log.d(StaticValues.LOG_TAG,"Launch weapon");
		isLaunched = true;
	}
	
	public void moveUpWeaponTarget(int botX, int botY) {
		if (!isLaunched) {
			int newTargetY = targetY+DEFAULT_TARGET_SPEED;
			int newTargetX = Double.valueOf(Math.sqrt((DEFAULT_TARGET_X*DEFAULT_TARGET_X)-(newTargetY * newTargetY))).intValue();
			if (targetX < 0)
				newTargetX = -newTargetX;
			if ((newTargetX >= -DEFAULT_TARGET_X && newTargetX <= DEFAULT_TARGET_X)
						&& (newTargetY >= -DEFAULT_TARGET_Y && newTargetY <= DEFAULT_TARGET_Y)){
				targetY += DEFAULT_TARGET_SPEED;
				targetX = newTargetX;
				this.setX(botX+targetX);
				this.setY(botY+targetY);			
			}
		}
	}
	
	public void moveDownWeaponTarget(int botX, int botY) {
		if (!isLaunched) {
			int newTargetY = targetY-DEFAULT_TARGET_SPEED;
			int newTargetX = Double.valueOf(Math.sqrt((DEFAULT_TARGET_X*DEFAULT_TARGET_X)-(newTargetY * newTargetY))).intValue();
			if (targetX < 0)
				newTargetX = -newTargetX;
			if ((newTargetX >= -DEFAULT_TARGET_X && newTargetX <= DEFAULT_TARGET_X)
						&& (newTargetY >= -DEFAULT_TARGET_Y && newTargetY <= DEFAULT_TARGET_Y)){
				targetY -= DEFAULT_TARGET_SPEED;
				targetX = newTargetX;
				this.setX(botX+targetX);
				this.setY(botY+targetY);			
			}
		}
	}
	
	public void centerTargetToCoordonates(int centerX, int centerY) {
		if (!isLaunched) {
			this.setX(centerX + targetX);
			this.setY(centerY + targetY);
		}
	}
	
	public void initWeapon(int botX, int botY) {
		isLaunched = false;
		isHide = false;
		targetX = DEFAULT_TARGET_X;
		targetY = 0;
		centerTargetToCoordonates(botX, botY);
	}

	public void moveRightWeaponTarget(int botX, int botY) {
		if (!isLaunched && targetX < 0) {
			targetX = -targetX;
			this.setX(botX+targetX);
			this.setY(botY+targetY);
		}
	}

	public void moveLeftWeaponTarget(int botX, int botY) {
		if (!isLaunched && targetX > 0) {
			targetX = -targetX;
			this.setX(botX+targetX);
			this.setY(botY+targetY);
		}
	}
	
}
